package com.example.myapp61.Mfragment;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;

import com.ejlchina.okhttps.HTTP;
import com.ejlchina.okhttps.HttpCall;
import com.ejlchina.okhttps.HttpResult;
import com.ejlchina.okhttps.Process;
import com.example.myapp61.Mfragment.Home.XList;
import com.example.myapp61.Mfragment.page.love.LoveMain;
import com.example.myapp61.MySqlDB.MsgModel;
import com.example.myapp61.R;
import com.example.myapp61.Util.HttpUtil;
import com.example.myapp61.Util.ToasUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.ContentValues.TAG;
import static android.content.Context.NOTIFICATION_SERVICE;

public class fragment2 extends Fragment {
    private Context context;
    private Button submit;
    private EditText content,contact;


    HTTP http = HTTP.builder()
            .callbackExecutor((Runnable run) -> {
                // 实际编码中可以吧 Handler 提出来，不需要每次执行回调都重新创建
                new Handler(Looper.getMainLooper()).post(run); // 在主线程执行
            })
            .baseUrl("https://yeqt.top:3005/ago")
            .build();

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment2, null);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }
    private void initView(View v) {
        submit = v.findViewById(R.id.submit);
        content = v.findViewById(R.id.conten);
        contact = v.findViewById(R.id.contact);
        submit.setOnClickListener(new View.OnClickListener() {
            private void on(IOException e) {
            }

            private void on(Process process) {
            }

            @Override
            public void onClick(View view) {
                //装箱
                Map<String, Object> params = new HashMap<>();
                params.put("messages", content.getText());
                params.put("rel", contact.getText());
                params.put("ui","Android");
                params.put("label",2);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                params.put("time",simpleDateFormat.format(date));
                //发送请求
                if (!TextUtils.isEmpty(content.getText().toString())||!TextUtils.isEmpty(contact.getText().toString())){
                    String s =  http.async("/androidSave")
                            .addBodyPara(params)
                            .post().getResult().getBody().toString();
                    //判断是否提交成功
                    if (Integer.parseInt(s)==1){
                        content.setText("");
                        contact.setText("");
                        ToasUtil.showMes(context,"增加成功了");
                    }else{ToasUtil.showMes(context,"提交失败");}
                }else{
                    ToasUtil.showMes(context,"不能空");
                }
            }
        });
    }
}
